package com.tg.app.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tg.app.R;
import com.tg.appcommon.android.ResourcesUtil;
import com.tg.data.helper.DeviceTypeHelper;

import com.tg.data.http.entity.BindHistryBean;

import java.util.List;

public class ApHistryDeviceAdapter extends RecyclerView.Adapter<ApHistryDeviceAdapter.ApDeviceViewHodler> {
    private final List<BindHistryBean> list;
    private final ApDeviceAdapterEx.OnAddWifiDeviceClickListener listener;


    public ApHistryDeviceAdapter(List<BindHistryBean> list, ApDeviceAdapterEx.OnAddWifiDeviceClickListener listener) {
        this.list = list;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ApHistryDeviceAdapter.ApDeviceViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.device_add_ap_item_new, parent, false);
        return new ApDeviceViewHodler(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ApHistryDeviceAdapter.ApDeviceViewHodler holder, final int position) {
        BindHistryBean sWifiAp = list.get(position);
        holder.itemView.setOnClickListener(v -> listener.onItemClick(position));
        String deviceType = getDeviceTypeName(sWifiAp.getDevice_type());
        holder.deviceWifiName.setText(deviceType.replace("\"", ""));
        holder.deviceUUID.setText(sWifiAp.getUuid().replace("\"", ""));
        holder.btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                listener.onItemClick(position);
            }
        });
    }

    public static String getDeviceTypeName(@NonNull String deviceType){
        if (DeviceTypeHelper.isCar(deviceType)) {
            if (DeviceTypeHelper.isBatteryDevice(deviceType)) {
                return ResourcesUtil.getString(com.appbase.custom.R.string.device_battery_car_4g);
            }
            return ResourcesUtil.getString(com.appbase.custom.R.string.device_car_4g);
        }

        if (DeviceTypeHelper.is4GDevice(deviceType)) {
            if (DeviceTypeHelper.isBattery4GDevice(deviceType)) {
                return ResourcesUtil.getString(com.appbase.custom.R.string.device_battery);
            }
            return ResourcesUtil.getString(com.appbase.custom.R.string.device_4g);
        }

        if (DeviceTypeHelper.isBatteryWifiDevice(deviceType)) {
            return ResourcesUtil.getString(R.string.wifi_device_battery);
        }

        if (DeviceTypeHelper.isWifiDevice(deviceType)){
            return ResourcesUtil.getString(com.appbase.custom.R.string.device_wifi);
        }
        return ResourcesUtil.getString(com.appbase.custom.R.string.camera);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


    public interface OnAddWifiDeviceClickListener {
        void onItemClick(int position);
    }

    public static class ApDeviceViewHodler extends RecyclerView.ViewHolder {
        TextView deviceWifiName;
        TextView deviceUUID;
        Button btnAdd;


        public ApDeviceViewHodler(View view) {
            super(view);
            deviceUUID = view.findViewById(R.id.device_add_ap_camera_uuid);
            deviceWifiName = view.findViewById(R.id.device_add_ap_camera_name);
            btnAdd = view.findViewById(R.id.device_add_ap_select);
        }
    }
}
